package com.caoustc.okhttplib.utils

import com.caoustc.okhttplib.okhttp.platform.Stat
import java.io.UnsupportedEncodingException
import java.net.URLEncoder
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*
import java.util.regex.Pattern

class StringUtils private constructor() {
    companion object {
        fun ToSBC(input: String): String {
            val c = input.toCharArray()
            for (i in c.indices) {
                if (c[i] == ' ') {
                    c[i] = '\u3000'
                } else if (c[i] < '\u007f') {
                    c[i] = (c[i] + 65248)
                }
            }
            return String(c)
        }

        @kotlin.jvm.JvmStatic
        fun isBlank(str: String?): Boolean {
            return str == null || str.trim { it <= ' ' }.length == 0
        }

        @kotlin.jvm.JvmStatic
        fun isEmpty(str: CharSequence?): Boolean {
            return str == null || str.length == 0
        }

        fun isEquals(actual: String?, expected: String?): Boolean {
            return ObjectUtils.isEquals(actual, expected)
        }

        fun length(str: CharSequence?): Int {
            return str?.length ?: 0
        }

        fun nullStrToEmpty(str: Any?): String {
            return if (str == null) "" else (if (str is String) str else str.toString())!!
        }

        fun capitalizeFirstLetter(str: String): String {
            if (isEmpty(str)) {
                return str
            }
            val c = str[0]
            return if (!Character.isLetter(c) || Character.isUpperCase(c)) str else StringBuilder(
                str.length
            )
                .append(Character.toUpperCase(c)).append(str.substring(1)).toString()
        }

        fun utf8Encode(str: String): String {
            return if (!isEmpty(str) && str.toByteArray().size != str.length) {
                try {
                    URLEncoder.encode(str, "UTF-8")
                } catch (e: UnsupportedEncodingException) {
                    throw RuntimeException("UnsupportedEncodingException occurred. ", e)
                }
            } else str
        }

        fun utf8Encode(str: String, defultReturn: String): String {
            return if (!isEmpty(str) && str.toByteArray().size != str.length) {
                try {
                    URLEncoder.encode(str, "UTF-8")
                } catch (e: UnsupportedEncodingException) {
                    defultReturn
                }
            } else str
        }

        fun getHrefInnerHtml(href: String?): String? {
            if (isEmpty(href)) {
                return ""
            }
            val hrefReg = ".*<[\\s]*a[\\s]*.*>(.+?)<[\\s]*/a[\\s]*>.*"
            val hrefPattern = Pattern.compile(hrefReg, Pattern.CASE_INSENSITIVE)
            val hrefMatcher = hrefPattern.matcher(href)
            return if (hrefMatcher.matches()) {
                hrefMatcher.group(1)
            } else href
        }

        fun htmlEscapeCharsToString(source: String): String {
            return if (isEmpty(source)) source else source.replace("&lt;".toRegex(), "<")
                .replace("&gt;".toRegex(), ">")
                .replace("&amp;".toRegex(), "&").replace("&quot;".toRegex(), "\"")
        }

        fun fullWidthToHalfWidth(s: String): String {
            if (isEmpty(s)) {
                return s
            }
            val source = s.toCharArray()
            for (i in source.indices) {
                if (source[i].toInt() == 12288) {
                    source[i] = ' '
                    // } else if (source[i] == 12290) {
                    // source[i] = '.';
                } else if (source[i].toInt() >= 65281 && source[i].toInt() <= 65374) {
                    source[i] = (source[i] - 65248)
                } else {
                    source[i] = source[i]
                }
            }
            return String(source)
        }

        fun halfWidthToFullWidth(s: String): String {
            if (isEmpty(s)) {
                return s
            }
            val source = s.toCharArray()
            for (i in source.indices) {
                if (source[i] == ' ') {
                    source[i] = 12288.toChar()
                    // } else if (source[i] == '.') {
                    // source[i] = (char)12290;
                } else if (source[i].toInt() >= 33 && source[i].toInt() <= 126) {
                    source[i] = (source[i] + 65248)
                } else {
                    source[i] = source[i]
                }
            }
            return String(source)
        }

        fun getMoblie(mobile: String): String {
            val str1 = mobile.substring(0, 3)
            val str2 = mobile.substring(7, 11)
            return "$str1****$str2"
        }

        fun stringToDate(str: String?): Date? {
            val sdf = SimpleDateFormat("yyyy-MM-dd") //小写的mm表示的是分钟
            var date: Date? = null
            try {
                date = sdf.parse(str)
            } catch (e: ParseException) {
                e.printStackTrace()
            }
            return date
        }

        @kotlin.jvm.JvmStatic
        fun isResultOk(result: String?, isError: Boolean?, desc: String?): Boolean {
            if (!isEmpty(result) && "ok".equals(result, ignoreCase = true)) {
                return true
            }
            //无敌的接口返回 "result":"成功"，健康上报，crm
            if (!isEmpty(result) && "成功".equals(result, ignoreCase = true)) {
                return true
            }
            // 又一个无敌接口 "desc": "成功"
            if ("成功".equals(desc, ignoreCase = true)) {
                return true
            }
            return (isError != null && !isError)
        }

        @kotlin.jvm.JvmStatic
        fun isResultSuccess(stat: Stat?): Boolean {
            return if (stat != null && stat.code != null && stat.code == 0) {
                true
            } else false
        }
    }

    init {
        throw AssertionError()
    }
}